package com.top.web.hooker;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.hooker.settings.Setting;
import com.tool.hooker.library.R;
import com.top.web.hooker.common.HookerIntent;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, AppSelectActivity.class);
                intent.putExtra(AppSelectActivity.FROM, HookerIntent.FROM_HOOKER);

                startActivity(intent);
            }
        });

        final Button debug = findViewById(R.id.debug);

        if (Setting.isDebug(this)) {
            debug.setText("关闭Debug");
        } else {
            debug.setText("开启Debug");
        }

        debug.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Setting.isDebug(getApplicationContext())) {
                    Setting.setDebug(getApplicationContext(), false);

                    debug.setText("开启Debug");
                } else {
                    Setting.setDebug(getApplicationContext(), true);

                    debug.setText("关闭Debug");
                }
            }
        });

        findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Setting.clear(getApplicationContext());
            }
        });

        findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), AppActivity.class);

                startActivity(intent);
            }
        });

        if (initSettings()) {
            initHook();
        }


        if (!"com.tool.hooker".equals(getPackageName())) {
            finish();
        }
    }


    private void initHook() {
        stopApp();
    }

    private void stopApp() {
        try {
            String stopApp = Setting.getStopApp(getApplicationContext());
            if (stopApp != null) {
                Process su = Runtime.getRuntime().exec("su");

                OutputStream outputStream = su.getOutputStream();
                outputStream.write((" am force-stop " + stopApp + "\n").getBytes());
                outputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private boolean initSettings() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            boolean b = Settings.System.canWrite(getApplicationContext());

            if (!b) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                startActivityForResult(intent, 1002);

                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
